// pages/Itinerary/ItineraryList/ItineraryList.js
import { getItineraryListForPage } from '../../api/home'
import { imageURL } from '../../../utils/request'

Page({
  data: {
    imageURL:'',
    itineraryList: [],
    page: 1,
    rows: 10,
    hasMore: true,
    loading: false
  },

  onLoad() {
    this.setData({imageURL})
    this.loadItineraryList()
  },
  goItineraryDetails(e){
    const itinerary_id = e.currentTarget.dataset.itinerary_id
    wx.navigateTo({
      url: `/pages/Itinerary/ItineraryDetails/ItineraryDetails?itinerary_id=${itinerary_id}`,
    })
  },
  // 加载行程列表
  async loadItineraryList() {
    if (!this.data.hasMore || this.data.loading) return

    this.setData({ loading: true })

    try {
      const res = await getItineraryListForPage({
        page: this.data.page,
        rows: this.data.rows,
        // is_recommend: 0
      })

      const newList = Array.isArray(res.data.list) ? res.data.list : []

      this.setData({
        itineraryList: [...this.data.itineraryList, ...newList],
        page: this.data.page + 1,
        hasMore: newList.length === this.data.rows,
        loading: false
      })
    } catch (err) {
      console.error('獲取行程列表失敗:', err)
      this.setData({ loading: false })
    }
  },

  // 触底事件
  onReachBottom() {
    this.loadItineraryList()
  },

  goSearch() {
    wx.navigateTo({
      url: '/pages/Itinerary/searchList/searchList?type=Itinerary'
    })
  }
})
